package com.yuan.zsschool.ui.me;

import android.content.Intent;
import android.os.Bundle;
import android.text.format.Formatter;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yuan.base.framework.BaseFrameworkActivity;
import com.yuan.base.utils.DialogUtil;
import com.yuan.base.utils.FileUtil;
import com.yuan.zsschool.AppContext;
import com.yuan.zsschool.BuildConfig;
import com.yuan.zsschool.Constant;
import com.yuan.zsschool.R;
import com.yuan.zsschool.ui.LoginActivity;
import com.yuan.zsschool.ui.account.ModifyPwdActivity;

import org.xutils.x;

import java.io.File;
import java.util.Objects;

/**
 * 设置
 */
public class SettingActivity extends BaseFrameworkActivity {

	// --------------------------控件--------------------------
	private TextView tv_cache_size;
	private TextView tv_version;
	// --------------------------变量--------------------------

	@Override
	protected int getLayoutId() {
		return R.layout.activity_setting;
	}

	@Override
	protected void bindViews() {
		tv_cache_size = findViewById(R.id.tv_cache_size);
		tv_version = findViewById(R.id.tv_version);

		setOnClickListener(R.id.ll_to_modify_pwd, v -> startActivity(new Intent(this, ModifyPwdActivity.class)));
		setOnClickListener(R.id.ll_logout, v -> {
			AppContext.getInstance().logout();
			Intent intent = new Intent(this, LoginActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			closeSelf();
		});
		setOnClickListener(R.id.ll_clear_cache, v -> {
			DialogUtil.confirm(this, "提示", "缓存包括头像、照片等图片数据\n确定要清除吗？",
					"是", (dialog, which) -> {
						x.image().clearCacheFiles();
						FileUtil.deleteFile(new File(AppContext.getSavePath(Constant.FilePath.APK_PATH)));
						FileUtil.deleteFile(Objects.requireNonNull(Glide.getPhotoCacheDir(AppContext.getInstance())));
						FileUtil.deleteFile(new File(AppContext.getSavePath(Constant.FilePath.TEMPORARY_PATH)));
						showToast("清除成功");
//						try {
//							AppContext.getDbUtils().dropDb();
//						} catch (DbException e) {
//							e.printStackTrace();
//						}
						initCacheSize();
					}, "否", null);
		});

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		tv_version.setText("V" + BuildConfig.VERSION_NAME);
		initCacheSize();
	}

	private void initCacheSize() {
		long count = 0;
		count += FileUtil.getFolderSize(new File(AppContext.getSavePath(Constant.FilePath.APK_PATH)));
		count += FileUtil.getFolderSize(Glide.getPhotoCacheDir(AppContext.getInstance()));
		count += FileUtil.getFolderSize(new File(AppContext.getSavePath(Constant.FilePath.TEMPORARY_PATH)));
		tv_cache_size.setText(Formatter.formatFileSize(this, count));
	}

}

